package com.cn.rocketmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("rocketmq")
public class ProducerController {

    @Autowired
    private RocketMQProducer rocketMQProducer;

    @Autowired
    private RocketMQTemplate rocketmqTemplate;

    @GetMapping("/test")
    public void test() {
        Message<String> msg = MessageBuilder.withPayload("Welcome, jxsr").build();
        rocketmqTemplate.send("jxsr", msg);
        log.info("消息发送成功");
    }


    @GetMapping("/msgWithValue")
    public void sendMsgWithValue(String value) {
        log.info("参数 value:{}", value);
        rocketMQProducer.sendMessageWithValue(value);
    }

    @GetMapping("/msgWithKey")
    public void sendMessageWithKey(String key, String value) {
        log.info("参数 key:{},value:{}", key, value);
        rocketMQProducer.sendMessageWithKey(key, value);
    }

    @GetMapping("/msgWithTag")
    public void sendMessageWithTag(String tag, String value) {
        log.info("参数 tag:{},value:{}", tag, value);
        rocketMQProducer.sendMessageWithTag(tag, value);
    }

    @GetMapping("/msgWithAll")
    public void sendMessageWithAll(String key, String tag, String value) {
        log.info("参数 key:{},tag:{},value:{}", key, tag, value);
        rocketMQProducer.sendMessageWithAll(key, tag, value);
    }
}
